<?php
namespace App\Models;

class SysUser extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "sys_user";
    }

    /**
     * 添加系统用户信息
     *
     * @param array $entity 系统用户信息
     *
     * @return int|false
     */
    public function add($entity) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $entity)) {
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取所有系统用户信息
     *
     * @param int $offset 偏移量
     * @param int $limit 获取条数
     *
     * @return array|false
     */
    public function gets($offset, $limit) {
        $query = "SELECT s1.*,s2.`name` AS role_name FROM `sys_user`AS s1 LEFT JOIN `sys_user_role` AS s2 ON s1.`role_id`=s2.id ORDER BY `id` DESC LIMIT :limit OFFSET :offset;";
        try {
            $ret = $this->db()->query($query, ['limit' => $limit, 'offset' => $offset])->fetchAll();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取系统用户信息的总记录数
     *
     * @return int|false
     */
    public function total() {
        $query = 'SELECT COUNT(*) AS `total` FROM `sys_user`';
        try {
            $ret = $this->db()->query($query)->fetch();
            if($ret !== false && is_array($ret) && isset($ret['total'])) {
                return intval($ret['total']);
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据登录名来获取对应的系统用户信息
     *
     * @param string $account 用户账户
     *
     * @return array|false
     */
    public function getByAccount($account) {
        $query = 'SELECT * FROM `sys_user` WHERE `account` = :account LIMIT 1';
        try {
            $ret = $this->db()->query($query, ['account' => $account])->fetch();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据用户的id值来获取用户信息
     *
     * @param int $id 用户id值
     *
     * @return array|false
     */
    public function get($id) {
        $query = 'SELECT * FROM `sys_user` WHERE `id` = :id LIMIT 1';
        try {
            $ret = $this->db()->query($query, ['id' => $id])->fetch();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 修改系统用户状态
     *
     * @param int $id id值
     *
     * @return int|false
     */
    public function changeStatus($id) {
        $id   = intval($id);
        $user = $this->get($id);
        if($user === false) {
            return false;
        }
        $entity = [
            'id'     => $id,
            'status' => $user['status'] == 1 ? 0 : 1,
        ];
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 修改系统用户信息
     *
     * @param array $entity 系统用户信息
     *
     * @return int|false
     */
    public function changeUserInfo($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 修改系统用户账户密码
     *
     * @param array $entity 系统用户信息
     *
     * @return int|false
     */
    public function resetPassword($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 修改系统用户资料
     *
     * @param array $entity 系统用户信息
     *
     * @return int|false
     */
    public function edit($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }
}
